﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCCN.BLL;
using SCCN.Utility;
using SCCN.DAL;

public partial class Admin_CategoryList : System.Web.UI.Page
{
    CategoryBLL categoryBll = CategoryBLL.getInstance();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRpt();
        }
    }

    void BindRpt()
    {
        SC_Category obj = new SC_Category();
        obj.CategoryName = this.txtName.Text;
        int rowCount = 0;
        this.rptList.DataSource = categoryBll.GetCategoryByCondition(obj, this.ApNetPager1.CurrentPageIndex, this.ApNetPager1.PageSize, out rowCount);
        this.rptList.DataBind();
        this.ApNetPager1.RecordCount = rowCount;
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (this.HCategoryId.Value.Length == 0)
        {
            if (categoryBll.GetCategoryByName(this.txtCategoryName.Text) != null)
            {
                this.lblAlert.Text = "该分类已存在！";
                return;
            }
            AddCategory();
        }
        else
        {
            UpdateCategory();
        }

    }

    /// <summary>
    /// 新增分类
    /// </summary>
    void AddCategory()
    {
        string categoryName = this.txtCategoryName.Text;
        SCCN.DAL.SC_Category obj = new SCCN.DAL.SC_Category();
        obj.CategoryName = this.txtCategoryName.Text;
        obj.IsDelete = false;
        if (categoryBll.AddCategory(obj))
        {
            HttpHelper.Alert("保存成功！", Page);
            BindRpt();
        }
        else
        {
            HttpHelper.Alert("保存失败！", Page);
        }
    }

    void UpdateCategory()
    {
        SCCN.DAL.SC_Category obj = new SCCN.DAL.SC_Category();
        int Id = 0;
        int.TryParse(this.HCategoryId.Value, out Id);
        obj.Id = Id;
        obj.CategoryName = this.txtCategoryName.Text;
        obj.IsDelete = false;
        if (categoryBll.UpdateCategory(obj))
        {
            HttpHelper.Alert("保存成功！", Page);
            BindRpt();
        }
        else
        {
            this.lblAlert.Text = "保存失败！";
        }
    }

    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        this.HCategoryId.Value = e.CommandArgument.ToString(); 
        int Id = 0;
        int.TryParse(this.HCategoryId.Value, out Id);
        if (e.CommandName == "Del")
        {
            if (categoryBll.DeleteCategory(Id))
            {
                HttpHelper.Alert("删除成功！", Page);
                BindRpt();
            }
            else
            {
                HttpHelper.Alert("删除失败！", Page);
            }
        }
        if (e.CommandName == "Edit")
        { 
            SC_Category obj = categoryBll.GetCategoryByID(Id);
            this.HCategoryId.Value = Id.ToString();
            this.txtCategoryName.Text = obj.CategoryName; 
        }
    }
    protected void ApNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindRpt();
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindRpt();
    }
}